package com.jf.hb.ui;

import com.jf.hb.HB;
import com.jf.hb.ui.interfaces.Menu;
import com.jf.hb.ui.interfaces.*;

import javax.swing.*;
import java.awt.*;
import java.io.File;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-08
 * Time:3:03
 */
public class JFHandler implements Runnable {
    private static JPanel panel;
    private CardLayout cardLayout;
    private int IFNUM = 0;
    private Interface interfaces[];
    private IF curInterface = IF.REGISTER;

    private File record;
    private File backupRecord;

    private String username;
    private String password;

    static public IF switchSignal = null;
    public static MENU_CHOICE menuChoice = null;
    private boolean isNew;



    public enum IF {
        REGISTER,
        LOGIN,
        MENU,
        TRICK,
        BLESSING,
        WISH
    }

    public enum MENU_CHOICE {
        NEW_GIFT,
        LUCKY_NUMBER,
        WISHLIST
    }

    public JFHandler() {
        //初始化界面和层叠布局
        cardLayout = new CardLayout();
        panel = new JPanel(cardLayout);
        IFNUM = IF.values().length;

        interfaces = new Interface[IFNUM];
        interfaces[IF.REGISTER.ordinal()] = new Register();
        interfaces[IF.LOGIN.ordinal()] = new Login();
        interfaces[IF.MENU.ordinal()] = new Menu();
        interfaces[IF.TRICK.ordinal()] = new Trick();
        interfaces[IF.BLESSING.ordinal()] = new Blessing();
        interfaces[IF.WISH.ordinal()] = new Wish();
        for (IF i : IF.values()) {
            panel.add(i.name(), interfaces[i.ordinal()]);
        }

        panel.add(IF.REGISTER.name(), interfaces[IF.REGISTER.ordinal()]);
        panel.add(IF.LOGIN.name(), interfaces[IF.LOGIN.ordinal()]);
        panel.add(IF.MENU.name(), interfaces[IF.MENU.ordinal()]);
        panel.add(IF.TRICK.name(), interfaces[IF.TRICK.ordinal()]);
        panel.add(IF.BLESSING.name(), interfaces[IF.BLESSING.ordinal()]);
        panel.add(IF.WISH.name(), interfaces[IF.WISH.ordinal()]);


        //关联记录文件
        record = HB.record;
        backupRecord = HB.backupRecord;
        //关联用户名和密码
        username = HB.username;
        password = HB.password;

        //初始化界面值
        if (password == null || password.isEmpty()) {
            curInterface = IF.REGISTER;
        } else {
            curInterface = IF.LOGIN;
        }

    }

    public JPanel getPanel() {
        return panel;
    }

    public void show() {
        cardLayout.show(panel, curInterface.name());
        interfaces[curInterface.ordinal()].start();
    }

    private void handle() {
        if (switchSignal != null) {
            switch (switchSignal) {
                case REGISTER:
                    curInterface = IF.TRICK;
                    isNew = true;
                    HB.writeUserInformation();
                    break;
                case LOGIN:
                    curInterface = IF.MENU;
                    break;
                case MENU:
                    switch (menuChoice) {
                        case NEW_GIFT:
                            ((Trick) interfaces[IF.TRICK.ordinal()]).init();
                            curInterface = IF.TRICK;
                            isNew = true;
                            break;
                        case LUCKY_NUMBER:
                            ((Blessing) interfaces[IF.BLESSING.ordinal()]).init();
                            curInterface = IF.BLESSING;
                            break;
                        case WISHLIST:
                            curInterface = IF.WISH;
                            break;
                    }
                    break;
                case TRICK:
                    curInterface = IF.BLESSING;
                    HB.writeLuckyNumber();
                    HB.setBlessingText();
                    ((Blessing) interfaces[IF.BLESSING.ordinal()]).init();
                    break;
                case BLESSING:
                    if (isNew) {
                        curInterface = IF.WISH;
                        isNew = false;
                    } else {
                        curInterface = IF.MENU;
                    }
                    break;
                case WISH:
                    curInterface = IF.MENU;
                    ((Wish) interfaces[IF.WISH.ordinal()]).init();
                    break;
            }
            switchSignal = null;
            show();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                handle();
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
